package com.wh.order.util;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.wh.basic.vo.StringUtils;
import com.wh.order.dao.OrderListMapper;
import com.wh.order.vo.OrderExportVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 订单导入效验规则
 */
@Component
public class orderImportExcelVerify implements IExcelVerifyHandler<OrderExportVo> {
    @Autowired
    private OrderListMapper orderListMapper;

    @Override
    public ExcelVerifyHandlerResult verifyHandler(OrderExportVo orderExportVo) {
        String orderSn = orderExportVo.getOrderSn();
        String trackingNumber = orderExportVo.getTrackingNumber();
        String orderStatus = orderExportVo.getOrderStatus();
        StringBuilder errorMsg = new StringBuilder();
        ExcelVerifyHandlerResult result = new ExcelVerifyHandlerResult();
        //忽略某些数据
        if (StringUtils.isAllBlank(orderSn) || "导出的订单总金额是:".equals(orderExportVo.getOrderSn()) || !"待发货".equals(orderStatus)) {
            return new ExcelVerifyHandlerResult(true);
        }
        //判读订单编号是否为空以及订单编号是否存在
        if (orderSn == null || orderSn.isEmpty()) {
            errorMsg.append("订单编号不能为空");
        } else {
            Boolean ordersave = orderListMapper.selectOrderSn(orderSn);
            if(ordersave==false){
                errorMsg.append("该订单编号并不存在，请检查是有误;");
            }
        }
        if(!orderStatus.equals("待发货")|| orderStatus.isEmpty()){
            errorMsg.append("该订单不是待发货订单，导入系统已经忽略该条数据！");

        }
        if (trackingNumber == null || trackingNumber.isEmpty()) {
            errorMsg.append("物流单号不能为空");
        }
        if (errorMsg.length() > 0) {
            result.setMsg(errorMsg.toString());
            result.setSuccess(false);
        } else {
            result.setSuccess(true);
        }
        return result;
    }
}
